﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;

namespace ExamSharedClasses {
    /// <summary>
    /// (k) ExamQuestion ist die Spezialisierung von Exam. Sie enthält zusätzlich
    /// die Fragen, ggf. die Antworten und eine Liste mit Punkten für jede Frage
    /// </summary>
    [Serializable()]
    public class ExamWithQuestions : Exam {
        public new const string TABLENAME = "exam_question";
        public const string QUESTION_ID = "question_id";
        public const string POINTS = "points";

        public List<Answer> AnswerList { get; set; }
        public List<int> PointsList { get; set; }
        
        public ExamWithQuestions() : base() {
            AnswerList = new List<Answer>();
            PointsList = new List<int>();
        }
        public ExamWithQuestions( List<Answer> answerList, List<int> pointsList , Exam exam)
            : base(exam) {
            this.AnswerList = answerList;
            this.PointsList = pointsList;
        }
        public ExamWithQuestions( ExamWithQuestions examQuestions )
            : base( examQuestions ) {
            this.AnswerList = examQuestions.AnswerList;
            this.PointsList = examQuestions.PointsList;
        }
        public ExamWithQuestions( Exam exam ) : base ( exam ) {
            this.AnswerList = new List<Answer>();
            this.PointsList = new List<int>();
        }

        public override string ToString() {
            StringBuilder sb = new StringBuilder( base.ToString() );//+ "ExamQuestions: " );
            if ( AnswerList != null ) {
                sb.Append( "Fragen gesamt: " + AnswerList.Count + ", " );
            }
            if ( GetMaxPoints() >= 0 )
                sb.Append( "max. Punkte: " + GetMaxPoints() + '\n' );
            return sb.ToString();
        }
        
        public string ToMoreDetailedString() {
            StringBuilder sb = new StringBuilder( base.ToString() + '\n');
            int i = 0, pointSum = 0;
            if ( AnswerList != null && PointsList != null ) {
                foreach ( Answer a in AnswerList ) {
                        Question q = a.AnswerQuestion;
                        sb.Append( " Frage Nr. " + (i+1)+ " " + q.ToShortInfoString() + " ");

                        int p = PointsList[i];
                        sb.Append( "Punkte: " + p.ToString() + "\n" );
                        pointSum += p;
                        i++;
                }
                sb.Append( "\nFragen gesamt: " + i + ", max. Punkte: " + pointSum + '\n' + '\n' );
            }
            return sb.ToString();
        }

        public string ToFullExamWithAnswersString() {
            StringBuilder sb = new StringBuilder( );

            sb.Append( " Klausur Nr.: " + ExamId + " Name: " + ExamName + " Datum: " + ExamWriteDate + '\n' );
            sb.Append( " Klasse: " + ExamClass.ClassName + " Modul: " + ExamModule.ModuleName + '\n' );
            sb.Append( " Dozent: " + ExamAuthor.UserEmail + " (" + ExamAuthor.UserId + ") " + '\n' );
            sb.Append( " Anz. d. Fragen: " + AnswerList.Count + ", max. Punkte: " + GetMaxPoints() + " Dauer (Min.): " + ( ExamDuration / 60 ) + '\n' );
            sb.Append( '\n' );

            sb.Append( '\n' );

            int i = 0;
            foreach ( Answer a in AnswerList ) {
                Question q = a.AnswerQuestion;
                sb.Append( q.ToMoreDetailedString() );
                sb.Append( "max Punkte: " + PointsList[i] + '\n' );
                sb.Append( '\n' );
                ++i;
            }
            return sb.ToString();
        }

        public int GetMaxPoints() {
            int sum = 0;
            foreach ( int i in PointsList ) {
                sum += i;
            }
            return sum;
        }
        public int GetAchievedPoints() {
            int i = 0, sum = 0;
            foreach ( Answer a in AnswerList ) {
                sum += a.GetAchievedPoints(PointsList[i]);
                i++;
            }
            return sum;
        }
     }
}
